#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Collections.Generic;
using System.Data;
using Flynn.Core.Activities;
using Flynn.Core.ActivityResults;
using Flynn.Core.TaskResults;
using Flynn.Core.Tasks;
using MbUnit.Framework;
using Rhino.Mocks;

namespace Flynn.Core.Tests.Tasks
{
    [TestFixture]
    public class UpdateDatabaseTaskFixture
    {
        private MockRepository _mocks;

        private IGetCurrentVersionActivity _getCurrentVersionActivity;
        private IFilterAndExecuteUpdatesActivity _filterAndExecuteUpdatesActivity;
        private ISetCurrentVersionActivity _setCurrentVersionActivity;
        private IUpdateDatabaseTask _updateDatabaseTask;

        [SetUp]
        public void SetUp()
        {
            _mocks = new MockRepository();

            _getCurrentVersionActivity = _mocks.CreateMock<IGetCurrentVersionActivity>();
            _filterAndExecuteUpdatesActivity = _mocks.CreateMock<IFilterAndExecuteUpdatesActivity>();
            _setCurrentVersionActivity = _mocks.CreateMock<ISetCurrentVersionActivity>();
            _updateDatabaseTask = new UpdateDatabaseTask(_getCurrentVersionActivity, _filterAndExecuteUpdatesActivity, _setCurrentVersionActivity);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetCurrentVersionActivity_Must_Not_Be_Null()
        {
            new UpdateDatabaseTask(null, _filterAndExecuteUpdatesActivity, _setCurrentVersionActivity);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FilterAndExecuteUpdatesActivity_Must_Not_Be_Null()
        {
            new UpdateDatabaseTask(_getCurrentVersionActivity, null, _setCurrentVersionActivity);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void SetCurrentVersionActivity_Must_Not_Be_Null()
        {
            new UpdateDatabaseTask(_getCurrentVersionActivity, _filterAndExecuteUpdatesActivity, null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Updating_Database_Project_Must_Not_Be_Null()
        {
            _updateDatabaseTask.UpdateDatabase(null, null, _mocks.Stub<IDbConnection>());
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Updating_Database_Connection_Must_Not_Be_Null()
        {
            _updateDatabaseTask.UpdateDatabase(new Project(), null, null);
        }

        [Test]
        public void Can_Update_Database()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                using (_mocks.Ordered())
                {
                    Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Return(GettingCurrentVersionResult.Success(string.Empty));
                    IEnumerable<Update> executedUpdates = new List<Update> { new Update("1"), new Update("2"), new Update("3") };
                    Expect.Call(_filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, string.Empty, string.Empty, connection)).Return(FilterAndExecuteUpdatesResult.Success(executedUpdates));
                    Expect.Call(_setCurrentVersionActivity.SetCurrentVersion(project, "3", connection)).Return(SettingCurrentVersionResult.Success());
                }
            }

            using (_mocks.Playback())
            {
                UpdatingDatabaseResult result = _updateDatabaseTask.UpdateDatabase(project, string.Empty, connection);

                Assert.IsFalse(result.Failed);
            }
        }

        [Test]
        public void Dont_Set_Current_Version_If_No_Updates_Have_Been_Executed()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                using (_mocks.Ordered())
                {
                    Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Return(GettingCurrentVersionResult.Success("1"));
                    Expect.Call(_filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, "1", string.Empty, connection)).Return(FilterAndExecuteUpdatesResult.Success(new List<Update>()));
                }
            }

            using (_mocks.Playback())
            {
                UpdatingDatabaseResult result = _updateDatabaseTask.UpdateDatabase(project, string.Empty, connection);

                Assert.IsFalse(result.Failed);
            }
        }

        [Test]
        public void Updating_Database_Fails_If_Getting_Current_Version_Throws_Exception()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Throw(new Exception("Failure message"));
            }

            using (_mocks.Playback())
            {
                UpdatingDatabaseResult result = _updateDatabaseTask.UpdateDatabase(project, null, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Updating_Database_Fails_If_Getting_Current_Version_Fails()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                GettingCurrentVersionResult gettingCurrentVersionResult = GettingCurrentVersionResult.Failure("Failure message");
                Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Return(gettingCurrentVersionResult);
            }

            using (_mocks.Playback())
            {
                UpdatingDatabaseResult result = _updateDatabaseTask.UpdateDatabase(project, null, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Updating_Database_Fails_If_Filtering_And_Executing_Updates_Throws_Exception()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Return(GettingCurrentVersionResult.Success("1"));
                Expect.Call(_filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, "1", string.Empty, connection)).Throw(new Exception("Failure message"));
            }

            using (_mocks.Playback())
            {
                UpdatingDatabaseResult result = _updateDatabaseTask.UpdateDatabase(project, string.Empty, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Updating_Database_Fails_If_Filtering_And_Executing_Updates_Fails()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Return(GettingCurrentVersionResult.Success("1"));
                Expect.Call(_filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, "1", string.Empty, connection)).Return(FilterAndExecuteUpdatesResult.Failure("Failure message"));
            }

            using (_mocks.Playback())
            {
                UpdatingDatabaseResult result = _updateDatabaseTask.UpdateDatabase(project, string.Empty, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Updating_Database_Fails_If_Setting_Current_Version_Throws_Exception()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Return(GettingCurrentVersionResult.Success(string.Empty));
                IEnumerable<Update> executedUpdates = new List<Update> { new Update("1"), new Update("2"), new Update("3") };
                Expect.Call(_filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, string.Empty, string.Empty, connection)).Return(FilterAndExecuteUpdatesResult.Success(executedUpdates));
                Expect.Call(_setCurrentVersionActivity.SetCurrentVersion(project, "3", connection)).Throw(new Exception("Failure message"));
            }

            using (_mocks.Playback())
            {
                UpdatingDatabaseResult result = _updateDatabaseTask.UpdateDatabase(project, string.Empty, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Updating_Database_Fails_If_Setting_CurrentVersion_Fails()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Return(GettingCurrentVersionResult.Success(string.Empty));
                IEnumerable<Update> executedUpdates = new List<Update> { new Update("1"), new Update("2"), new Update("3") };
                Expect.Call(_filterAndExecuteUpdatesActivity.FilterAndExecuteUpdates(project, string.Empty, string.Empty, connection)).Return(FilterAndExecuteUpdatesResult.Success(executedUpdates));
                Expect.Call(_setCurrentVersionActivity.SetCurrentVersion(project, "3", connection)).Return(SettingCurrentVersionResult.Failure("Failure message"));
            }

            using (_mocks.Playback())
            {
                UpdatingDatabaseResult result = _updateDatabaseTask.UpdateDatabase(project, string.Empty, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }
    }
}